package com.huabo.contract.service.impl;


import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.PageInfo;
import com.huabo.contract.config.DateBaseConfig;
import com.huabo.contract.oracle.entity.*;
import com.huabo.contract.oracle.mapper.TblBiPageMapper;
import com.huabo.contract.oracle.mapper.TblYyPriceMapper;
import com.huabo.contract.oracle.mapper.TblYyReportModelMapper;
import com.huabo.contract.oracle.mapper.TblYyXdfCompanyMapper;
import com.huabo.contract.service.TblyyxdfCompanyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@Service
public class TblyyxdfCompanyServiceImpl implements TblyyxdfCompanyService {

    @Resource
    private TblYyXdfCompanyMapper tblYyXdfCompanyMapper;

    @Resource
    private TblYyPriceMapper tblYyPriceMapper;
    @Resource
    private TblYyReportModelMapper tblYyReportModelMapper;
    @Resource
    private TblBiPageMapper tblBiPageMapper;



    @Override
    public Map<String, Object> findByCompay(Integer pageNumber, Integer pageSize, String teamid, String fxtype, String companyname, String token) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                TblStaffUtil staff = DealUserToken.parseUserToken(token);
                if (staff == null) {
                    resultMap.put("code", "0");
                    resultMap.put("msg", "用户已失效！");
                    return resultMap;
                }
                if (pageNumber == null) {
                    pageNumber = 1;
                }
                if (pageSize == null) {
                    pageSize = 15;
                }
                PageInfo<TblYyXdfCompany> pageInfo = new PageInfo<TblYyXdfCompany>();
                if (teamid != null && teamid.trim().length() > 0) {
                    TblYyXdfCompany company = new TblYyXdfCompany();
                    company.setCompanyname(companyname);
                    company.setFxtype(fxtype);
                    company.setTeamid(new BigDecimal(teamid));
                    company.setStaffid(staff.getStaffid());
                    pageInfo.setCurrentPage(pageNumber);
                    pageInfo.setPageSize(pageSize);
                    pageInfo.setCondition(company);
                    pageInfo.setTlist(tblYyXdfCompanyMapper.selectListByPageInfo(pageInfo));
                    pageInfo.setTotalRecord(tblYyXdfCompanyMapper.selectCountByPageInfo(pageInfo));
                    List<TblYyXdfCompany> list = pageInfo.getTlist();
                    List<TblYyXdfCompany> newlist = new ArrayList<TblYyXdfCompany>();
                    if (list != null && list.size() > 0) {
                        for (TblYyXdfCompany tblyyCompany : list) {
                            List<TblYyPrice> list2 = tblYyPriceMapper.findByIs(tblyyCompany.getReportid());
                            tblyyCompany.setList(list2);
                            newlist.add(tblyyCompany);
                        }
                        pageInfo.setTlist(newlist);
                    }
                } else {
                    TblYyXdfCompany company = new TblYyXdfCompany();
                    company.setCompanyname(companyname);
                    company.setFxtype(fxtype);
                    company.setStaffid(staff.getStaffid());
                    company.setOrgid(staff.getCurrentOrg().getOrgid());
                    pageInfo.setCurrentPage(pageNumber);
                    pageInfo.setPageSize(pageSize);
                    pageInfo.setCondition(company);
                    pageInfo.setTlist(tblYyXdfCompanyMapper.findBySqlPage(pageInfo));
                    pageInfo.setTotalRecord(tblYyXdfCompanyMapper.findCountBySqlPage(pageInfo));
                    List<TblYyXdfCompany> list = pageInfo.getTlist();
                    List<TblYyXdfCompany> newlist = new ArrayList<TblYyXdfCompany>();
                    if (list != null && list.size() > 0) {
                        for (TblYyXdfCompany tblyyCompany : list) {
                            List<TblYyPrice> list2 = tblYyPriceMapper.findByIs(tblyyCompany.getReportid());
                            tblyyCompany.setList(list2);
                            newlist.add(tblyyCompany);
                        }
                        pageInfo.setTlist(newlist);
                    }
                }
                resultMap.put("code", "1");
                resultMap.put("msg", "访问接口成功");
                resultMap.put("data", pageInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                TblStaffUtil staff = DealUserToken.parseUserToken(token);
                if (staff == null) {
                    resultMap.put("code", "0");
                    resultMap.put("msg", "用户已失效！");
                    return resultMap;
                }
                if (pageNumber == null) {
                    pageNumber = 1;
                }
                if (pageSize == null) {
                    pageSize = 15;
                }
                PageInfo<TblYyXdfCompany> pageInfo = new PageInfo<TblYyXdfCompany>();
                if (teamid != null && teamid.trim().length() > 0) {
                    TblYyXdfCompany company = new TblYyXdfCompany();
                    company.setCompanyname(companyname);
                    company.setFxtype(fxtype);
                    company.setTeamid(new BigDecimal(teamid));
                    company.setStaffid(staff.getStaffid());
                    pageInfo.setCurrentPage(pageNumber);
                    pageInfo.setPageSize(pageSize);
                    pageInfo.setCondition(company);
                    pageInfo.setTlist(tblYyXdfCompanyMapper.selectListByPageInfo(pageInfo));
                    pageInfo.setTotalRecord(tblYyXdfCompanyMapper.selectCountByPageInfo(pageInfo));
                    List<TblYyXdfCompany> list = pageInfo.getTlist();
                    List<TblYyXdfCompany> newlist = new ArrayList<TblYyXdfCompany>();
                    if (list != null && list.size() > 0) {
                        for (TblYyXdfCompany tblyyCompany : list) {
                            List<TblYyPrice> list2 = tblYyPriceMapper.findByIs(tblyyCompany.getReportid());
                            tblyyCompany.setList(list2);
                            newlist.add(tblyyCompany);
                        }
                        pageInfo.setTlist(newlist);
                    }
                } else {
                    TblYyXdfCompany company = new TblYyXdfCompany();
                    company.setCompanyname(companyname);
                    company.setFxtype(fxtype);
                    company.setStaffid(staff.getStaffid());
                    company.setOrgid(staff.getCurrentOrg().getOrgid());
                    pageInfo.setCurrentPage(pageNumber);
                    pageInfo.setPageSize(pageSize);
                    pageInfo.setCondition(company);
                    pageInfo.setTlist(tblYyXdfCompanyMapper.findBySqlPage(pageInfo));
                    pageInfo.setTotalRecord(tblYyXdfCompanyMapper.findCountBySqlPage(pageInfo));
                    List<TblYyXdfCompany> list = pageInfo.getTlist();
                    List<TblYyXdfCompany> newlist = new ArrayList<TblYyXdfCompany>();
                    if (list != null && list.size() > 0) {
                        for (TblYyXdfCompany tblyyCompany : list) {
                            List<TblYyPrice> list2 = tblYyPriceMapper.findByIs(tblyyCompany.getReportid());
                            tblyyCompany.setList(list2);
                            newlist.add(tblyyCompany);
                        }
                        pageInfo.setTlist(newlist);
                    }
                }
                resultMap.put("code", "1");
                resultMap.put("msg", "访问接口成功");
                resultMap.put("data", pageInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return resultMap;
        }
    }

    @Override
    public Map<String, Object> saveOrupdateYYCompany(String pageid, String priceid, String token, TblYyXdfCompany company) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                TblStaffUtil staff = DealUserToken.parseUserToken(token);
                if (staff == null) {
                    resultMap.put("code", "0");
                    resultMap.put("msg", "用户已失效！");
                    return resultMap;
                }
                TblYyReportModel model = new TblYyReportModel();
                model.setOrgid(staff.getCurrentOrg().getOrgid());
                model.setPriceid(priceid);
                model.setStaffid(staff.getStaffid());
                model.setReportname(company.getCompanyname());
                tblYyReportModelMapper.saveModel(model);
//            Set<TblBiPage> pagess =new HashSet<>();
//            if(pageid!=null && pageid.length()>0) {
//                List<TblBiPage> pageChilds=tblBiPageMapper.findBypageid(pageid);
//                pagess =new HashSet<>(pageChilds);
//            }

                company.setReportid(model.getReportid());
                company.setOrgid(staff.getCurrentOrg().getOrgid());
                company.setStaffid(staff.getStaffid());
                company.setCreatedate(new Date());
                // company.setPages(pagess);
                tblYyXdfCompanyMapper.insertCompany(company);
                resultMap.put("code", "1");
                resultMap.put("msg", "访问接口成功");
                resultMap.put("data", company);
            } catch (Exception e) {
                e.printStackTrace();
            }

            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                TblStaffUtil staff = DealUserToken.parseUserToken(token);
                if (staff == null) {
                    resultMap.put("code", "0");
                    resultMap.put("msg", "用户已失效！");
                    return resultMap;
                }
                TblYyReportModel model = new TblYyReportModel();
                model.setOrgid(staff.getCurrentOrg().getOrgid());
                model.setPriceid(priceid);
                model.setStaffid(staff.getStaffid());
                model.setReportname(company.getCompanyname());
                tblYyReportModelMapper.saveModel(model);
//            Set<TblBiPage> pagess =new HashSet<>();
//            if(pageid!=null && pageid.length()>0) {
//                List<TblBiPage> pageChilds=tblBiPageMapper.findBypageid(pageid);
//                pagess =new HashSet<>(pageChilds);
//            }

                company.setReportid(model.getReportid());
                company.setOrgid(staff.getCurrentOrg().getOrgid());
                company.setStaffid(staff.getStaffid());
                company.setCreatedate(new Date());
                // company.setPages(pagess);
                tblYyXdfCompanyMapper.insertCompany(company);
                resultMap.put("code", "1");
                resultMap.put("msg", "访问接口成功");
                resultMap.put("data", company);
            } catch (Exception e) {
                e.printStackTrace();
            }

            return resultMap;
        }
    }
}
